package handlers

import (
	"github.com/gin-gonic/gin"
	"github.com/goccy/go-json"
	"time"
	"weibo-stats/cache"
	"weibo-stats/models"
)

func List(c *gin.Context) {
	var wei []models.Wei

	redis_key := "list_novel"
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Find(&wei)
		jsonData, _ := json.Marshal(wei)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &wei)
	}

	c.JSON(200, gin.H{
		"code": 500,
		"msg":  "查询成功",
		"data": wei,
	})
	return
}
func Detail(c *gin.Context) {
	id := c.Query("id")
	var wei models.Wei

	redis_key := "detail_novel" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Where("id = ?", id).First(&wei)
		jsonData, _ := json.Marshal(wei)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &wei)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": wei,
	})
	return

}
